import * as postdata from '../../../data/posts-data.js'
const app = getApp();
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id;
    const data = postdata.postList[id];
    this.setData({ postId: id });
    this.setData({ postData: data });
    that = this;
    //获取收藏信息
    const postsCollected = wx.getStorageSync('posts_collected');
    if (postsCollected) {
      const postCollected = postsCollected[id];
      this.setData({
        collected: postCollected
      })
    } else {
      const postsCollected = {}
      postsCollected[id] = false;
      wx.setStorageSync('posts_collected', postsCollected);
    }

    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId == that.data.postId){
      that.setData({
        isPlayingMusic:true,
      })
    }

    that.setMusicMonitor();


  },

  setMusicMonitor:()=>{
    wx.onBackgroundAudioPlay(() => {
      that.setData({
        isPlayingMusic: true
      })

      app.globalData.g_isPlayingMusic = true;
      app.globalData.g_currentMusicPostId = that.data.postId;
    });

    wx.onBackgroundAudioPause(() => {
      that.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    });

    wx.onBackgroundAudioStop(() => {
      that.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    });
  },
  
  onCollectionTap: () => {
    var postsCollected = wx.getStorageSync('posts_collected');
    var postid = that.data.postId;
    var postCollected = postsCollected[that.data.postId];
    postCollected = !postCollected;
    postsCollected[that.data.postId] = postCollected;
    
    // wx.showToast({
    //   title: postCollected?'收藏成功':'取消收藏',
    //   duration:1000,
    //   icon:'success',
    //   mask:true,
    // })
    that.showModal(postsCollected, postCollected);
   
  },

  showModal: (postsCollected, postCollected) => {
    wx.showModal({
      title: '收藏',
      content: postCollected ? '是否收藏该文章' : '是否取消收藏该文章',
      showCancel: true,
      cancelText: "取消",
      cancelColor: '#333',
      confirmText: "确认",
      confirmColor: "#405f80",
      success: (res) => {
        if (res.confirm) {
          wx.setStorageSync('posts_collected', postsCollected);
          that.setData({
            collected: postCollected
          })
        }
      }
    })
  },


  

  onShareTap: (event) => {
    const itemlist=[
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博",
    ]
    wx.showActionSheet({
      itemList: itemlist,
      itemColor:'#405f80',
      success:(res)=>{
        //res.cancel  res.tapIndex数组元素的序号，从0开始
        wx.showModal({
          title: '用户' + itemlist[res.tapIndex],
          content: '用户是否取消?'+res.cancel+'现在无法实现分享功能',
        })
      }
    })
  },

  onMusicTap:(event)=>{
    const currentPostid = that.data.postId;
    const isPlayingMusic = that.data.isPlayingMusic;
    if (!isPlayingMusic){
      that.setData({
        isPlayingMusic:true
      })

      wx.playBackgroundAudio({
        dataUrl: postdata.postList[currentPostid].music.url,
        title: postdata.postList[currentPostid].music.title,
        coverImgUrl: postdata.postList[currentPostid].music.coverImg,
      });
    }else{
      wx.pauseBackgroundAudio();
      that.setData({
        isPlayingMusic: false
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})